package com.internetCafes.spms.web.sys.bizservice.impl.sms;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.internetCafes.spms.core.exception.RRException;
import com.internetCafes.spms.core.exception.RRExceptionCodeEnum;
import com.internetCafes.spms.web.sys.bizservice.sms.SysSmsUserSendConfBizService;
import com.internetCafes.spms.web.sys.common.cont.SmsSendConfConst;
import com.internetCafes.spms.web.sys.entity.sms.SysSmsUserSendConf;
import com.internetCafes.spms.web.sys.model.sms.podo.SysSmsUserSendConfDo;
import com.internetCafes.spms.web.sys.model.sms.podo.SysSmsUserSendConfPo;
import com.internetCafes.spms.web.sys.model.sms.req.SysSmsUserSendConfReq;
import com.internetCafes.spms.web.sys.model.sms.res.SysSmsUserSendConfFunctionCodeRes;
import com.internetCafes.spms.web.sys.service.sms.ISysSmsUserSendConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统短信发送配置业务接口实现
 *
 * @author HHKJ-SERVER-ZJ 2022/5/30 12:09
 */
@Service
public class SysSmsUserSendConfBizServiceImpl implements SysSmsUserSendConfBizService {

    @Autowired
    private ISysSmsUserSendConfService smsSendConfService;

    @Override
    public List<String> listMobileByFunctionCode(String functionCode) {
        SysSmsUserSendConfDo smsUserSendConfDo = new SysSmsUserSendConfDo();
        smsUserSendConfDo.setFunctionCode(functionCode);
        return smsSendConfService.listPoNoSqlParse(smsUserSendConfDo)
                .stream()
                .map(SysSmsUserSendConfPo::getMobile)
                .collect(Collectors.toList());
    }

    @Override
    public List<SysSmsUserSendConfFunctionCodeRes> listFunctionCode() {
        return Arrays.stream(SmsSendConfConst.FunctionCode.values())
                .map(code -> new SysSmsUserSendConfFunctionCodeRes(code.getCode(), code.getFunction()))
                .collect(Collectors.toList());
    }

    @Override
    public boolean saveConf(SysSmsUserSendConfReq req) {
        // 删除原有配置，保存新的内容信息
        if (CollectionUtil.isEmpty(req.getUserIdList())) {
            throw new RRException(RRExceptionCodeEnum.PARAM_ERROR);
        }
        smsSendConfService.remove(new LambdaQueryWrapper<SysSmsUserSendConf>()
                .in(SysSmsUserSendConf::getUserId, req.getUserIdList()));

        // 验证用户是否有手机号


        List<SysSmsUserSendConf> confDoList = new ArrayList<>();
        // 校对配置编码是否存在
        if (CollectionUtil.isEmpty(req.getFunctionCodeList())) {
            List<String> functionCodeList
                    = Arrays.stream(SmsSendConfConst.FunctionCode.values())
                    .map(SmsSendConfConst.FunctionCode::getCode)
                    .collect(Collectors.toList());

            if (!functionCodeList.containsAll(req.getFunctionCodeList())) {
                throw new RRException(RRExceptionCodeEnum.PARAM_ERROR);
            }
            SysSmsUserSendConf confDo;
            for (Long userId : req.getUserIdList()) {
                for (String functionCode : req.getFunctionCodeList()) {
                    confDo = new SysSmsUserSendConf();
                    confDo.setUserId(userId);
                    confDo.setFunctionCode(functionCode);
                    confDoList.add(confDo);
                }
            }
        }

        if (CollectionUtil.isNotEmpty(confDoList)) {
            smsSendConfService.saveBatch(confDoList);
        }
        return true;
    }
}
